<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VideoController extends Controller
{
    public function index()
    {


    }
    public function create()
    {
        $oExpert=Expert::all();
        $oVideo=Video::all();
        return view('admin.video-create')
            ->with('oVideo',$oVideo)
            ->with('oExpert',$oExpert);


    }
    public function store(Request $request)
    {
        $datas = $request->all();
        $rules=[
            'title'=>'required|min:2',
            'introduce'=>'required|min:15',
            'url'=>'required',
        ];
        $message=[
            'title.required'=>'姓名不能为空',
            'title.min'=>'姓名太短，至少要有:min个字符',
            'introduce.required'=>'描述不能为空',
            'introduce.min'=>'描述太短，至少要有:min个字符',
            'url.required'=>'路径不能为空',
        ];
        $validate = Validator::make($datas,$rules,$message);
        if($validate->fails())
        {
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }else {
            if ($request->hasFile('thumb')) {
                $thumb = $request->thumb;
                $filename = uniqid() . '.' . $thumb->extension();
                $thumb->move(public_path() . '/upload', $filename);
                $datas["thumb"] = '/upload/' . $filename;
            }
            Video::create($datas);
            return json_encode(['success' => true, 'msg' => 'success']);
        }
    }
    public function edit(Video $video)
    {
        $oExpert=Expert::all();
        return view('admin.video-edit')
            ->with('video',$video)
            ->with('oExpert',$oExpert);

    }


    public  function update($id,Request $request,Video $video)
    {
        $datas = $request->all();
        $rules=[
            'title'=>'required|min:2',
            'introduce'=>'required|min:15',
            'url'=>'required',
        ];
        $message=[
            'title.required'=>'姓名不能为空',
            'title.min'=>'姓名太短，至少要有:min个字符',
            'introduce.required'=>'描述不能为空',
            'introduce.min'=>'描述太短，至少要有:min个字符',
            'url.required'=>'姓名不能为空',
        ];
        $validate = Validator::make($datas,$rules,$message);
        if($validate->fails())
        {
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $video = Video::find($id);
        if ($request->hasFile('thumb')) {
            $thumb = $request->thumb;
            $filename = uniqid() . '.' . $thumb->extension();
            $thumb->move(public_path() . '/upload', $filename);
            $video["thumb"] = '/upload/' . $filename;
        }
        $video->title = $request['title'];
        $video->type = $request['leixing'];
        $video->description = $request['introduce'];
        $video->kaishishijian = $request['kaishishijian'];
        $video->jieshushijian = $request['jieshushijian'];
        $video->save();
        return json_encode(['success'=>true,'msg'=>'修改成功']);

    }
    public function destroy(Video $video)
    {
        $video->delete();
        return json_encode(['msg'=>'删除成功']);
    }
    public function VideoList()
    {
        $oVideo = Video::all();
        return view('admin.video')
            ->with('oVideo',$oVideo);
    }
    public function jiangtang(){

        return view('front.conf');

    }
    public function review(){
        $ovideo=video::orderby('created_at','desc')->paginate(5);
        return view('front.review')->with('ovideo',$ovideo);
    }
    public function reviewinfo($id){
        $video=Video::find($id);
        $ovideo=Video::where('id','!=',$id)->take(1)->get();
        return view('front.review-info')->with('video',$video)->with('ovideo',$ovideo);
    }

}
